<template>
    <div class="header-container" v-if="$route.meta.isHeader">
        <!-- 头部区域 -->
        <div class="header">
            <div class="wrapper">
                <ul class="shortcut-menu">
                    <template v-if="!userStore.profile">
                        <li><a href="/login">请 先登录</a></li>
                        <li><a href="/register">免费注册</a></li>
                    </template>
                    <template v-else>
                        <li><a href="/me">{{ userStore.profile.user.username }}</a></li>
                        <li><a href="javascript:;" @click="handleLogout">退出登录</a></li>
                        <li><a href="/user/order">我的订单</a></li>
                        <li>
                            <a href="/cart" class="cart-wrap">
                                <span>购物车</span>
                                <span class="cart-num" v-if="cartStore.validTotal < 100">{{ cartStore.validTotal }}</span>
                                <span class="cart-num" v-else>99+</span>

                            </a>
                        </li>
                    </template>
                    <li><a href="">帮助中心</a></li>
                    <li><a href="">在线客服</a></li>
                    <li>
                        <a href="">
                            <i class="iconfont icon-shouji"></i>手机版
                        </a>
                    </li>
                </ul>
            </div>
        </div>

        <!-- 主导航区域 -->
        <div class="main-nav" v-if="$route.meta.isMainNav">
            <div class="wrapper">
                <!-- logo区域 -->
                <!-- 1. logo里面首先放一个h1标签，目的是为了提权，告诉搜索引擎，这个地方很重要。
                2. h1里面再放一个链接，可以返回首页的，把logo的背景图片给链接即可。
                3.为了搜索引擎收录我们，我们链接里面要放文字(网站名称），但是文字不要显示出来。
                    方法1 : text-indent移到盒子外面 ( text-indent:-9999px)，然后overflowhidden，淘宝的做法。●方法2∶直接给font-size:0;就看不到文字了，京东的做法。
                4．最后给链接一个title属性，这样鼠标放到logo上就可以看到提示文字了。 -->

                <h1 class="logo">
                    <a href="/" title="三叉戟优购">三叉戟优购</a>
                </h1>

                <!-- 搜索区域 + 导航开始 -->
                <div class="right-wrap">
                    
                    <!-- 搜索区域开始 -->
                    <div class="search-wrap">
                        <div class="left-box">宝贝</div>
                        <div class="input-box">
                            <el-input v-model="keyword" @input="inputHandler" class="search-input" type="text" placeholder="搜索宝贝" clearable/>

                            <!-- 搜索历史区域开始 -->
                            <div class="search-history-wrap" v-if="keyword == ''">
                                <!-- 头部区域开始 -->
                                <div class="title-box">
                                    <span class="title">搜索历史：</span>
                                    <div class="clear-history-box" @click="userStore.clearSearchHistory()">
                                        <i class="iconfont icon-shanchu"></i>
                                        <span class="clear-history-txt">清空历史</span>
                                    </div>
                                </div>
                                <!-- 头部区域结束 -->


                                <!-- 内容区域开始 -->
                                <div class="content-box">
                                    <div class="content-item" v-for="(item, index) in userStore.validSearchHistoryList" :key="index">
                                        <span class="txt">{{item}}</span>
                                        <i class="iconfont icon-chengyi" @click="userStore.deleteSearchHistory(item)"></i>
                                    </div>
                                </div>
                                <!-- 内容区域结束 -->
                            </div>
                            <!-- 搜索历史区域结束 -->
                        </div>
                        <div class="right-box" @click="handlerSearch">
                            <span class="search-btn">搜索</span>
                        </div>
                    </div>
                    <!-- 搜索区域结束 -->


                    <!-- 导航区域开始 -->
                    <div class="nav">
                        <ul>
                            <li v-for="(item, index) in navList" :key="index"><a :href="`/goods-list?keyword=${item}`">{{ item }}</a></li>
                        </ul>
                    </div>
                    <!-- 导航区域结束 -->


                    <!-- 搜索建议区域开始 -->
                    <div class="search-suggest-wrap" v-if="searchResults.length > 0">
                        <ul>
                            <template v-for="(item, index) in searchResults" :key="index">
                                <li v-if="index < 10" @click="handlerSearchSuggest(item.value)">{{ item.value }}</li>
                            </template>
                        </ul>

                        <div class="bottom-box">
                            <span class="close-txt" @click="handlerCloseSearchSuggest">关闭</span>
                        </div>
                    </div>
                    <!-- 搜索建议区域结束 -->
                </div>
                <!-- 搜索区域 + 导航结束 -->
            </div>
        </div>
    </div>
</template>


<script setup>
import { ref } from "vue"
import { useRouter } from 'vue-router'
import { reqSearchSuggestCompletionAPI } from "@/apis/goods"

import {useUserStore} from "@/stores/modules/user"
import {useCartStore} from "@/stores/modules/cart"
import {useOrderStore} from "@/stores/modules/order"

import { ElNotification, ElMessageBox } from 'element-plus';

// --------------------- 定义全局变量开始 ---------------------
// 定义用户存储
const userStore = useUserStore()

// 定义购物车存储
const cartStore = useCartStore()

// 定义订单存储
const orderStore = useOrderStore()

// 搜索关键字
const keyword = ref('')

// 路由器
const router = useRouter()

// 延时器
const timer = ref(null)

// 推荐搜索列表
const searchResults = ref([])

// 定义导航变量
const navList = ref([
    '手机',
    '电脑',
    '空调',
    '洗衣机',
    '电视',
    '美妆',
    '女鞋',
    '裙子',
    '生鲜',
    '鲜花',
    '箱包',
])
// --------------------- 定义全局变量结束 ---------------------



// --------------------- 输入事件处理开始 ---------------------
const inputHandler = () => {
	// ------------------ 防抖处理 ------------------
	// 清除延时器
	clearTimeout(timer.value)
	// 重新启动延时器
	timer.value = setTimeout(() => {
		getSearchList()
	}, 500)
}
// --------------------- 输入事件处理结束 ---------------------

const handlerSearch = () => {
    searchResults.value = []
    router.push({path: '/goods-list', query:{keyword: keyword.value}})
}



// --------------------- 处理搜索建议开始 ---------------------
const handlerSearchSuggest = (value) => {
    keyword.value = value
    searchResults.value = []
}
// --------------------- 处理搜索建议结束 ---------------------



// --------------------- 关闭搜索建议开始 ---------------------
const handlerCloseSearchSuggest = () => {
    searchResults.value = []
}
// --------------------- 关闭搜索建议结束 ---------------------



// ------------- 根据搜索关键词,搜索商品建议列表开始 -------------
const getSearchList = async () => {
	if (keyword.value == '') {
		searchResults.value = []
		return
	}
	
	const result = await reqSearchSuggestCompletionAPI(keyword.value)
	searchResults.value = result.data.search_suggests != null ? result.data.search_suggests : []
	
	userStore.addSearchHistory(keyword.value)
}
// ------------- 根据搜索关键词,搜索商品建议列表结束 -------------



// ---------------------- 处理退出功能开始 ----------------------
const handleLogout = () => {
    ElMessageBox.confirm('确定要退出吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
        // // 清空用户信息
		userStore.clearProfile()

        // 清空收货地址信息
        userStore.clearDeliveryAddr()

        // 清空订单信息
        orderStore.clearCreateOrderParams()

        // // 清空购物车信息
        cartStore.clearCart()

        // 清空商品搜索历史

        ElNotification({type: 'success', message: '退出成功'})

        // 跳转登录页面
        setTimeout(() => {
            router.push({path: '/login'})
        }, 1500)
    }).catch(() => {
    })
}
// ---------------------- 处理退出功能结束 ----------------------
</script>
<style scoped lang="scss">
.header-container {
    position: fixed;
    width: 100%;
    z-index: 101;
    .header {
        height: 52px;
        background-color: $main-color;
        width: 100%;
        .wrapper {
            .shortcut-menu {
                height: 52px;
                display: flex;
                justify-content: flex-end;
                align-items: center;
                li {
                    a {
                        padding: 0 22px;
                        font-size: 14px;
                        border-right: 1px solid white;
                        color: white;
                        .iconfont {
                            color: white;
                        }
                    }
                    .cart-wrap {
                        position: relative;
                        .cart-num {
                            position: absolute;
                            top: -10px;
                            left: 55px;

                            color: $main-color;
                            font-size: 12px;
                            background-color: #fff;
                            padding: 3px 8px;
                            border-radius: 8px;
                            border: 1px solid $main-color;
                        }
                    }
                    &:first-child a {
                        padding-left: 0;
                    }
                    &:last-child a {
                        padding-right: 0;
                        border-right: none;
                    }
                }
            }
        }
    }

    // 主导航区域
    .main-nav {
        background-color: #fff;
        .wrapper {
            height: 130px;
            display: flex;
            align-items: center;
            // logo区域
            .logo {
                a {
                    display: block;
                    width: 300px;
                    height: 130px;
                    background-image: url(@/assets/images/logo.png);
                    background-size: 100% 100%;
                    font-size: 0;
                }
            }

            // 右边区域
            .right-wrap {
                position: relative;
                // 搜索区域
                .search-wrap {
                    display: flex;
                    align-items: center;
                    width: 750px;
                    height: 48px;
                    border-radius: 25px;
                    border: 2px solid $main-color;
                    .left-box {
                        padding: 8px 0;
                        width: 60px;
                        display: flex;
                        justify-content: center;
                        border-right: 1px solid #F3F0F0;
                    }
                    .input-box {
                        flex: 1;
                        padding-left: 6px;
                        position: relative;
                        .search-input {
                            width: 100%;
                            height: 45px;
                            border: none
                        }

                        &:hover .search-history-wrap {
                            display: block;
                        }

                        ::v-deep(.el-input .el-input__wrapper) {
                            box-shadow: none;
                        }

                        // 搜索历史区域
                        .search-history-wrap {
                            display: none;
                            position: absolute;
                            left: 0;
                            top: 45px;
                            width: 589px;
                            border: 2px solid $main-color;
                            padding-top: 10px;
                            border-top: 0;
                            border-bottom-left-radius: 10px;
                            border-bottom-right-radius: 10px;
                            overflow: hidden;
                            background-color: #fff;
                            .title-box {
                                display: flex;
                                justify-content: space-between;
                                padding: 10px;
                                font-size: 14px;
                                color: #999;
                                .clear-history-box {
                                    cursor: pointer;
                                }
                            }

                            .content-box {
                                display: flex;
                                flex-wrap: wrap;
                                padding: 10px 0;
                                font-size: 14px;
                                .content-item {
                                    cursor: pointer;
                                    padding: 5px 2px 5px 10px;
                                    margin: 5px;
                                    border: 1px solid #999;
                                    border-radius: 4px;
                                    .txt {
                                        padding-right: 6px;
                                        &:hover {
                                            color: $main-color;
                                        }
                                    }
                                    .icon-chengyi {
                                        &:hover {
                                            color: $main-color;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    .right-box {
                        width: 100px;
                        height: 48px;
                        line-height: 48px;
                        background-color: $main-color;
                        text-align: center;
                        cursor: pointer;
                        border-top-right-radius: 25px;
                        border-bottom-right-radius: 25px;


                        .search-btn {
                            color: #fff;
                        }
                    }
                }

                // 导航区域
                .nav {
                    margin-top: 10px;
                    padding-left: 15px;
                    ul {
                        display: flex;
                        li {
                            a {
                                padding: 0 16px;
                                font-size: 16px;
                                color: #333;
                                font-size: 13px;

                                &:hover {
                                    color: $main-color;
                                }
                            }

                            &:first-child a {
                                padding-left: 0;
                            }
                            &:last-child a {
                                padding-right: 0;
                            }
                        }
                    }
                }

                // 搜索建议区域
                .search-suggest-wrap {
                    position: absolute;
                    left: 62px;
                    top: 48px;
                    width: 587px;
                    background-color: #fff;
                    z-index: 100;
                    border: 2px solid $main-color ;
                    border-top: none;
                    border-bottom-left-radius: 10px;
                    border-bottom-right-radius: 10px;
                    overflow: hidden;
                    ul {
                        li {
                            cursor: pointer;
                            padding: 12px 10px;
                            border-bottom: 1px solid #ccc ;
                            color: #333;

                            &:hover {
                                background-color: #E5FAF7;
                                color: $main-color;
                            }

                            &:last-child {
                                border-bottom: none;
                            }
                        }
                    }

                    .bottom-box {
                        display: flex;
                        justify-content: flex-end;
                        height: 30px;
                        line-height: 30px;
                        background-color: $main-color;
                        .close-txt {
                            font-size: 14px;
                            color: #fff;
                            cursor: pointer;
                            padding: 0 20px;
                        }
                    }
                }
            }
        }
    }
}
</style>